package problema033;

public class DigitCancelingFractions {
	
	static int[] num1 = new int[10], num2 = new int[10];
	
	public static void main(String[] args){
		System.out.println(digitCanceling());
	}
	
	public static double digitCanceling(){
		int[][] sol = new int[4][2];
		int pos = 0;
		double div = 0;
		for(double den = 10; den < 99; den++){
			for(double num = 10; num < den; num++){
				div = num / den;
				num1 = new int[10]; num2 = new int[10];
				ponerArreglos((int)num, (int)den);
				for(int k = 0; k < num1.length; k++){
					if(num1[k] == 1 && num2[k] == 2){
						num1[k] = 0; num2[k] = 0;
						if(chequear(num1, num2, div)){
							System.out.println( num + " " + den);
							sol[pos][0] = (int) num;
							sol[pos][1] = (int) den;
							pos++;
							};
					}else{
						if(num1[k] == 2 && num2[k] == 1){
							num1[k] = 0; num2[k] = 0;
							if(chequear(num1, num2, div)){
								System.out.println( num + " " + den);
								sol[pos][0] = (int) num;
								sol[pos][1] = (int) den;
								pos++;
								}
							}
						}
					}
				}
			}
		double num = 1, den = 1;
		for(int i = 0; i < 4; i++){
			num *= sol[i][0];
			den *= sol[i][1];
		}
		return num/den * 10000;
	}
		
	
	public static boolean chequear(int[] num1, int[] num2, double div){
		int i = 0, j = 0;
		for(; i < num1.length; i++){
			if(num1[i] != 0) break;
		}
		for(; j < num2.length; j++){
			if(num2[j] != 0) break;
		}
		double div2 = (double) i / (double) j;
		if(div == div2){
			return true;
		}
		return false;
	}
	
	
	public static void ponerArreglos(int num, int den){
		//System.out.println(num + " " + den);		
		int pos = 0;
		pos = num % 10;
		num1[pos] = 1;
		pos = num /10; 
		num1[pos] = 2;

		/*for(int i = 0; i < num1.length; i++){
			System.out.print(num1[i] + " ");
		}System.out.println();*/
		pos = den % 10;
		num2[pos] = 1;
		pos = den /10; 
		num2[pos] = 2;
		
		/*for(int i = 0; i < num1.length; i++){
			System.out.print(num2[i] + " ");
		}System.out.println();*/
	}
	
}
